/**
 * 
 */
package com.wordpress.sharebyblog;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordpress.sharebyblog.world.domain.City;
import com.wordpress.sharebyblog.world.domain.Country;
import com.wordpress.sharebyblog.world.domain.WorldServiceRequest;
import com.wordpress.sharebyblog.world.domain.WorldServiceResponse;
import com.wordpress.sharebyblog.world.exception.WorldServiceException;
import com.wordpress.sharebyblog.world.services.WorldService;

/**
 * @author Bharat Mehta
 *
 */

@Controller
@RequestMapping("/worldservice")
public class WorldServiceController {
	
	@Autowired
	private WorldService worldService;
	
	

	@RequestMapping(value={"/listregionnames"}, method= RequestMethod.GET)
	public WorldServiceResponse<List<String>> listRegionNames(HttpServletRequest request, HttpServletResponse response) throws WorldServiceException {
		List<String> regionNames =  worldService.findRegions();
		return new WorldServiceResponse<List<String>>(regionNames);
	}
	
	
	@RequestMapping(value={"/country"},method=RequestMethod.GET)
	public @ResponseBody WorldServiceResponse<List<Country>> findCountry(HttpServletRequest request, HttpServletResponse response) throws WorldServiceException {
		WorldServiceRequest serviceRequest = new WorldServiceRequest(request.getParameterMap());
		List<Country> countries = new ArrayList<Country>();
		if(serviceRequest.contains(WorldServiceRequest.COUNTRYCODE)){
			Country country = worldService.findCountryByCode(serviceRequest.getRequest().get(WorldServiceRequest.COUNTRYCODE));
			countries.add(country);
		}else if(serviceRequest.contains(WorldServiceRequest.REGIONNAME)){
			List<Country> countriesByRegion = worldService.findCountriesByContinent(serviceRequest.getRequest().get(WorldServiceRequest.REGIONNAME));
			countries.addAll(countriesByRegion);
		}
		return new WorldServiceResponse<List<Country>>(countries);
	}
	
	@RequestMapping(value={"/city"},method=RequestMethod.GET)
	public @ResponseBody WorldServiceResponse<List<City>> findCity(HttpServletRequest request, HttpServletResponse response) throws WorldServiceException {
		WorldServiceRequest serviceRequest = new WorldServiceRequest(request.getParameterMap());
		List<City> cities = new ArrayList<City>();
		if(serviceRequest.contains(WorldServiceRequest.COUNTRYCODE)){
			List<City> citiesByCountry = worldService.findCitiesByCountry(serviceRequest.getRequest().get(WorldServiceRequest.COUNTRYCODE));
			cities.addAll(citiesByCountry);
		}
		return new WorldServiceResponse<List<City>>(cities);
	}
	
	@RequestMapping(value={"/test"},method=RequestMethod.GET)
	public @ResponseBody WorldServiceResponse<String> test(HttpServletRequest request, HttpServletResponse response) throws WorldServiceException {
		String s = "FU";
		return new WorldServiceResponse<String>(s);
	}


	/**
	 * @return the worldService
	 */
	public WorldService getWorldService() {
		return worldService;
	}


	/**
	 * @param worldService the worldService to set
	 */
	public void setWorldService(WorldService worldService) {
		this.worldService = worldService;
	}
	
	
	

}
